/**
 * @Description: test
 * @Author: Hu Outstanding
 * @Date: 2022/2/20 9:19
 */
package com.aaa.three.service.impl;

import com.aaa.three.mapper.AdminQueryOfficeMapper;
import com.aaa.three.pojo.query.AdminInfoDept;
import com.aaa.three.pojo.query.AdminInfoOffice;
import com.aaa.three.pojo.query.DeptState;
import com.aaa.three.result.AjaxResult;
import com.aaa.three.service.AdminQueryOfficeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminQueryOfficeServiceImpl implements AdminQueryOfficeService {

    @Autowired
    private AdminQueryOfficeMapper adminQueryOffice;


    @Override
    public AjaxResult queryDept() {
        List<AdminInfoDept> queryDept = adminQueryOffice.queryDept();
        return AjaxResult.success(queryDept);


    }

    @Override
    public AjaxResult getOffice(int id) {
        AdminInfoOffice getOffices = adminQueryOffice.getOffice(id);
        return AjaxResult.success(getOffices);
    }

    @Override
    public AjaxResult queryOffice() {
        List<AdminInfoOffice> queryOffices = adminQueryOffice.queryOffice();

        return AjaxResult.success(queryOffices);
    }

    @Override
    public AjaxResult listOffice(int id) {
        List<AdminInfoOffice> listOffice = adminQueryOffice.listOffice(id);

        return AjaxResult.success(listOffice);

    }

    @Override
    public AjaxResult queryState() {
        List<DeptState> queryState= adminQueryOffice.queryState();
        return AjaxResult.success(queryState);

    }

    @Override
    public AjaxResult updateOffice(AdminInfoOffice adminInfoOffice) {

        Integer did = adminInfoOffice.getDid();
        System.out.println("did"+"-----"+did);
        int dstate =  adminQueryOffice.getDstate(did);
        if (dstate == 1){

            int i = adminQueryOffice.updateOffice(adminInfoOffice);
            if (i > 0 ){
                return AjaxResult.success();
            }
            return AjaxResult.fail("修改失败");
        }
        return AjaxResult.fail("部门未启用，请联系部长");




    }

    @Override
    public AjaxResult deleteOffice(int id) {
        int i = adminQueryOffice.deleteOffice(id);
        if (i > 0 ){
            return AjaxResult.success();
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult addOffice(AdminInfoOffice adminInfoOffice) {

        Integer did = adminInfoOffice.getDid();
        int dstate =  adminQueryOffice.getDstate(did);

        if (dstate == 1){

            String name = adminInfoOffice.getOname();

            int q = adminQueryOffice.countName(name);

            if (q == 0 ){
                int i = adminQueryOffice.addOffice(adminInfoOffice);
                if (i > 0 ){
                    int oid = adminInfoOffice.getId();
                    System.out.println(oid);
                    int a = adminQueryOffice.addRole(oid);
                    int b = adminQueryOffice.addArole(oid);
                    return AjaxResult.success();
                }
                return AjaxResult.fail("添加失败");
            }
            return AjaxResult.fail("科室以存在");
        }
        return AjaxResult.fail("部门未启用，请联系部长");



    }
}
